package com.qyhl.module_activities.act.album;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.recyclerview.widget.RecyclerView;
import androidx.recyclerview.widget.StaggeredGridLayoutManager;

import com.bumptech.glide.Glide;
import com.luck.picture.lib.tools.ScreenUtils;
import com.makeramen.roundedimageview.RoundedImageView;
import com.qyhl.module_activities.R;
import com.qyhl.module_activities.R2;
import com.qyhl.module_activities.common.ActUrl;
import com.qyhl.webtv.basiclib.base.BaseFragment;
import com.qyhl.webtv.basiclib.utils.ContextUtilts;
import com.qyhl.webtv.basiclib.utils.network.EasyHttp;
import com.qyhl.webtv.basiclib.utils.network.callback.SimpleCallBack;
import com.qyhl.webtv.basiclib.utils.network.exception.ApiException;
import com.qyhl.webtv.commonlib.entity.act.ActivityImageBean;
import com.qyhl.webtv.commonlib.utils.view.imagebrowser.MNImageBrowser;
import com.weavey.loading.lib.LoadingLayout;
import com.zhy.adapter.recyclerview.CommonAdapter;
import com.zhy.adapter.recyclerview.MultiItemTypeAdapter;
import com.zhy.adapter.recyclerview.base.ViewHolder;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

import butterknife.BindView;

/**
 * 活动专题
 */
public class ActAlbumFragment extends BaseFragment {
    @BindView(R2.id.album_recyclerView)
    RecyclerView mAlbumRecyclerView;
    @BindView(R2.id.act_album_loading)
    LoadingLayout mAlbumLoading;
    private int imgW;
    private ArrayList<ActivityImageBean> mImages;
    private CommonAdapter<ActivityImageBean> mAdapter;
    private int id;

    public static ActAlbumFragment newInstance(ArrayList<ActivityImageBean> mImages, int id) {
        ActAlbumFragment fragment = new ActAlbumFragment();
        Bundle bundle = new Bundle();
        bundle.putParcelableArrayList("images", mImages);
        bundle.putInt("id", id);
        fragment.setArguments(bundle);
        return fragment;
    }

    @Override
    protected View createView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        return inflater.inflate(R.layout.act_fragment_act_album, container, false);
    }

    @Override
    protected void create() {
        mImages = getArguments().getParcelableArrayList("images");
        if (mImages == null) {
            mImages = new ArrayList<>();
        }
        id = getArguments().getInt("id");
        initView();
    }

    @Override
    protected void setListener() {

    }

    @Override
    protected void onFirstVisible() {

    }

    private void initView() {
        imgW = (ScreenUtils.getScreenWidth(getActivity()) - ScreenUtils.dip2px(getActivity(), 20)) / 2;
        mAlbumLoading.setStatus(LoadingLayout.Loading);
        if (mImages.isEmpty()) {
            mAlbumLoading.setErrorText("暂无数据");
            mAlbumLoading.setReloadButtonText("点击刷新");
            mAlbumLoading.setErrorImage(R.drawable.error_content);
            mAlbumLoading.setStatus(LoadingLayout.Error);
        } else {
            mAlbumLoading.setStatus(LoadingLayout.Success);
        }
        mAlbumLoading.setOnReloadListener(new LoadingLayout.OnReloadListener() {
            @Override
            public void onReload(View view) {
                getData();
            }
        });
        mAlbumRecyclerView.setLayoutManager(new StaggeredGridLayoutManager(2, StaggeredGridLayoutManager.VERTICAL));

        mAlbumRecyclerView.setAdapter(mAdapter = new CommonAdapter<ActivityImageBean>(getContext(), R.layout.act_item_activity_album, mImages) {
            @Override
            protected void convert(ViewHolder holder, ActivityImageBean activityImageBean, int position) {
                RoundedImageView imageView = holder.getView(R.id.album_iamge);
                ViewGroup.LayoutParams params = imageView.getLayoutParams();
                params.height = getRandomHeight(activityImageBean);
                imageView.setLayoutParams(params);

                Glide.with(getContext())
                        .load(activityImageBean.getUrl())
                        .into(imageView);
            }
        });

        mAdapter.setOnItemClickListener(new MultiItemTypeAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(View view, RecyclerView.ViewHolder holder, int position) {
                toImages(view, position);
            }

            @Override
            public boolean onItemLongClick(View view, RecyclerView.ViewHolder holder, int position) {
                return false;
            }
        });


    }

    private int getRandomHeight(ActivityImageBean activityImageBean) {//得到item的高度
        int r;
        if (activityImageBean.getImageWidth() != 0 && activityImageBean.getImageHeight() != 0) {
            float ratio = (float) activityImageBean.getImageWidth() / imgW;
            r = (int) (activityImageBean.getImageHeight() / ratio);
        } else {
            Random random = new Random();
            r = 350 + random.nextInt(200);
        }
        return r;
    }


    public void toImages(View v, int page) {
        ArrayList<String> result = new ArrayList<>();
        for (int i = 0; i < mImages.size(); i++) {
            result.add(mImages.get(i).getUrl());
        }
        MNImageBrowser.showImageBrowser(getContext(), v, page, result);
    }

    @Override
    protected void onVisible() {

    }

    @Override
    protected void onHide() {

    }

    private void getData() {
        mAlbumLoading.setStatus(LoadingLayout.Loading);
        EasyHttp.get(ActUrl.ACTIVITY_ALBUM)
                .params("actId", String.valueOf(id))
                .execute(new SimpleCallBack<List<ActivityImageBean>>() {
                    @Override
                    public void onError(ApiException e) {
                        if (getActivity() != null && !getActivity().isFinishing()) {
                            mAlbumLoading.setErrorText(ContextUtilts.getInstance().getmContext().getString(R.string.error_str));
                            mAlbumLoading.setReloadButtonText("点击刷新");
                            mAlbumLoading.setErrorImage(R.drawable.error_content);
                            mAlbumLoading.setStatus(LoadingLayout.Error);
                        }
                    }

                    @Override
                    public void onSuccess(List<ActivityImageBean> activityImageBeans) {
                        if (getActivity() != null && !getActivity().isFinishing()) {
                                if (activityImageBeans== null || activityImageBeans.isEmpty()) {
                                    mAlbumLoading.setErrorText("暂无数据");
                                    mAlbumLoading.setReloadButtonText("点击刷新");
                                    mAlbumLoading.setErrorImage(R.drawable.error_content);
                                    mAlbumLoading.setStatus(LoadingLayout.Error);
                                } else {
                                    mImages.clear();
                                    mImages.addAll(activityImageBeans);
                                    mAdapter.notifyDataSetChanged();
                                    mAlbumLoading.setStatus(LoadingLayout.Success);
                                }
                            } else {
                                mAlbumLoading.setErrorText(ContextUtilts.getInstance().getmContext().getString(R.string.error_str));
                                mAlbumLoading.setReloadButtonText("点击刷新");
                                mAlbumLoading.setErrorImage(R.drawable.error_content);
                                mAlbumLoading.setStatus(LoadingLayout.Error);
                            }
                    }
                });

    }
}
